/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Communication;


import TCP.ListenSocket;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author grevin
 */
public class Communica extends JFrame implements ActionListener {
    
    private BufferedWriter writer;
    private ListenSocket ls;
    private JButton bReceive;
    private JButton bSend;
    private JLabel messrec;
    private JLabel messend;
    private JTextArea textRec;
    private JTextArea textToSend;

    public Communica(BufferedWriter writer, ListenSocket ls) {
        this.writer = writer;
        this.ls = ls;
        ls.setCom(this);
        initComponent();
    }


    
    @Override
    public void actionPerformed(ActionEvent e){
            try {
                writer.write(textToSend.getText());
                writer.newLine();
                writer.flush();
               // writer.close();
            } catch (IOException ex) {
                Logger.getLogger(Communica.class.getName()).log(Level.SEVERE, null, ex);
            }
	//System.out.println(textToSend.getText());
    }

    public JTextArea getTextRec() {
        return textRec;
    }
    
    
    
    private void initComponent(){
        this.messend = new JLabel("message to send");
        this.messrec = new JLabel("received message");
        this.bReceive = new JButton("Receive");
        bReceive.addActionListener(this);
        this.bSend  = new JButton("Send");
        bSend.addActionListener(this);
        
        this.textRec = new JTextArea(5,20);
        this.textToSend = new JTextArea(5,20);
        this.setLayout(new GridLayout(3,2));
        this.add(this.messend);
        this.add(this.textToSend);
        this.add(this.bSend);
        this.add(this.bReceive);
        this.add(this.messrec);
        this.add(this.textRec);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        ls.start();                
        // the JFrame is visible now
        this.setVisible(true);
        }

}


